package com.shelpe.services.scheduler.item.keyword;


import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;

import com.shelpe.services.scheduler.dto.RptKeywordDto;
import com.shelpe.services.scheduler.mongorepository.UnrecoverableErrorRepository;
import com.shelpe.services.scheduler.po.KeywordStrTemp;
import com.shelpe.services.scheduler.po.SyncLog;
import com.shelpe.services.scheduler.syncclient.KeywordClient;
import com.shelpe.services.scheduler.util.Util;

public class InsightKeywordProcessor implements ItemProcessor<List<KeywordStrTemp>, SyncLog>{

	private static final Logger logger = LoggerFactory.getLogger(InsightKeywordProcessor.class);
	
	@Autowired
	private KeywordClient keyClient;
	
	@Autowired
	private UnrecoverableErrorRepository errorRep;
	
	@Override
	public SyncLog process(List<KeywordStrTemp> item) throws Exception {
		// TODO Auto-generated method stub
	
		RptKeywordDto rptDto = new RptKeywordDto();
		if(item.size() > 0){//item是能保证不为null的
			
			long userId = item.iterator().next().getUserId();
			if(errorRep.countByUserId(userId) > 0){
				return null;
			}
			
			StringBuilder sb = new StringBuilder();
			int size = item.size();
			
			for(int index = 0; index < size-1; index++){
				sb.append(item.get(index).getWord());
				sb.append(",");
			}
			
			sb.append(item.get(size - 1).getWord());
			rptDto = keyClient.syncInsightKeywordDataByUser(userId, 0, sb.toString());	
		}
		
		return Util.convert(rptDto);
	}

}
